import Vue from 'vue'
import VueI18n from 'vue-i18n'
import config from '@/config'

Vue.use(VueI18n)
// 自动根据浏览器系统语言设置语言
const navLang = sessionStorage.getItem('locale') || navigator.language
let lang = config.i18n.defaultLanguage || 'zh-CN'
// 标记是否需要单独加载默认语言
let flag = true
// 需要加载的语言
const messages = {}
if (config.i18n.enabled === 1 && config.i18n && config.i18n.languageList) {
  for (let key in config.i18n.languageList) {
    if (key === navLang) {
      lang = key
      flag = false
    }
    let ownLang = require('./lang/' + key)
    let iviewLang = require('view-design/src/locale/lang/' + key)
    messages[key] = Object.assign(ownLang.default, iviewLang.default)
  }
}
if (flag) {
  let ownLang = require('./lang/' + lang)
  let iviewLang = require('view-design/src/locale/lang/' + lang)
  messages[lang] = Object.assign(ownLang.default, iviewLang.default)
  sessionStorage.setItem('locale', lang)
}
Vue.config.lang = lang
// vue-i18n 6.x+写法
Vue.locale = () => {}
const i18n = new VueI18n({
  locale: lang,
  messages
})
export default i18n
